package com.adfframeworks.labs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.adfframeworks.labs.dao.EmployeeDAO;
import com.adfframeworks.labs.domain.Employee;
import com.adfframeworks.labs.service.EmployeeService;

@Service("EmployeeService")
public class EmployeeServiceImpl implements EmployeeService {

	private EmployeeDAO employeeDAO;
	private TransactionTemplate txTemplate;

	@Autowired
	@Qualifier("employeeDAOImpl")
	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	@Autowired
	public void setTxTemplate(TransactionTemplate txTemplate) {
		this.txTemplate = txTemplate;
	}

	@Override
	public void create(final String firstName, final String lastName,
			final String emailAdress, final String password, final Integer type) {
		txTemplate.execute(new TransactionCallback<Void>() {
			public Void doInTransaction(TransactionStatus txStatus) {
				try {
					employeeDAO.create(firstName, lastName, emailAdress,
							password, type);
				} catch (RuntimeException e) {
					txStatus.setRollbackOnly();
					throw e;
				}
				return null;
			}
		});
	}

	@Override
	public Employee getEmployee(Integer id) {
		return employeeDAO.getEmployee(id);
	}

	@Override
	public Employee getEmployee(String emailAdress) {
		return employeeDAO.getEmployee(emailAdress);
	}

	@Override
	public List<Employee> listAllEmployees() {
		return employeeDAO.listAllEmployees();
	}

	@Override
	public void delete(Integer id) {
		employeeDAO.delete(id);
	}

	@Override
	public void delete(String emailAdress) {
		employeeDAO.delete(emailAdress);
	}

	@Override
	public void update(Integer id, String firstName, String lastName,
			String emailAdress, String password, Integer type) {
		employeeDAO.update(id, firstName, lastName, emailAdress, password, type);
	}

}
